package com.ruoyi.stat.toilet.logic;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class HttpLogic {
    private static String getEnvAddr(ISysConfigService configService) {
        String url = "http://116.205.156.171/devapi";
        if (Objects.nonNull(configService)) {
            String configUrl = configService.selectConfigByKey("zj_url");
            url = Objects.isNull(configUrl) ? url : configUrl;
        }
        log.info(" api.url {}", url);
        System.out.println(StrUtil.format(" api.url {}", url));
        return url.trim();
    }


    private static Map<String, String> getEnvHeaders(ISysConfigService configService) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "123");
        headers.put("tenant_id", "56");
        headers.put("secret", "UsE0ZzYsfZvkezEGCppGh7Os2E50P9B0");
        if (Objects.nonNull(configService)) {
            String authorization = configService.selectConfigByKey("zj_authorization");
            String tenant = configService.selectConfigByKey("zj_tenant_id");
            String secret = configService.selectConfigByKey("zj_secret");
            if (!authorization.isEmpty())
                headers.put("Authorization", authorization);
            if (!tenant.isEmpty())
                headers.put("tenant_id", tenant);
            if (!secret.isEmpty())
                headers.put("secret", secret);
        }


        return headers;
    }


    public static String getBody(ISysConfigService configService, String url, String params) {
        String _url = getEnvAddr(configService).concat(url.trim());
        System.out.println(_url);
        Map<String, String> headers = getEnvHeaders(configService);
        HttpResponse response = HttpRequest.get(_url).headerMap(headers, true).body(params).execute();
        return response.body();
    }


    public static String postBody(ISysConfigService configService, String url, String params) {
        String _url = getEnvAddr(configService).concat(url.trim());
        System.out.println(_url);
        Map<String, String> headers = getEnvHeaders(configService);
        HttpResponse response = HttpRequest.post(_url).headerMap(headers, true).body(params).execute();
        return response.body();
    }

    public static <T> R<T> getData(ISysConfigService configService, String url, String params, Class<T> cls) {
        System.out.println("--------params:"+params);
        String body = getBody(configService, url, params);
        System.out.println("--------getData:"+body);
        JSONObject json = new JSONObject(body);
        if (json.getStr("code").equals("0")) {
            return R.ok(json.getStr("msg"), json.get("data", cls));
        }
        return R.fail(json.getStr("msg"));
    }

    public static <T> R<List<T>> getList(ISysConfigService configService, String url, String params, Class<T> cls) {
        System.out.println("--------params:"+params);
        String body = getBody(configService, url, params);
        System.out.println("--------getList:"+body);
        JSONObject json = new JSONObject(body);
        if (json.getStr("code").equals("0")) {
            JSONArray arr = new JSONArray(json.getStr("data"));
            List<T> list = new ArrayList<>();
            for (int i = 0; i < arr.size(); i++) {
                list.add(arr.get(i, cls));
            }
            return R.ok(json.getStr("msg"), list);
        }
        return R.fail(json.getStr("msg"));
    }
}
